package cc.mrbird.febs.accounting.service.impl;

import cc.mrbird.febs.accounting.entity.Company;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.accounting.entity.Subject;
import cc.mrbird.febs.accounting.mapper.SubjectMapper;
import cc.mrbird.febs.accounting.service.ISubjectService;
import cc.mrbird.febs.common.entity.Strings;
import cc.mrbird.febs.common.utils.FebsUtil;
import cc.mrbird.febs.utils.PermissionUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import lombok.RequiredArgsConstructor;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 *  Service实现
 *
 * @author baoyinlei
 * @date 2021-09-28 23:26:51
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements ISubjectService {

    private final SubjectMapper subjectMapper;

    @Override
    public IPage<Subject> findSubjects(QueryRequest request, Long projectId) {
        LambdaQueryWrapper<Subject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Subject::getIsDeleted, 0);
        queryWrapper.eq(Subject::getProjectId, projectId);
        Page<Subject> page = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(page, queryWrapper);
    }

    @Override
    public List<Subject> findSubjects(Long projectId) {
	    LambdaQueryWrapper<Subject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Subject::getIsDeleted, 0);
        queryWrapper.eq(Subject::getProjectId, projectId);
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createSubject(Subject subject) {
        subject.setSubjectCode(UUID.randomUUID().toString());
        subject.setCreateBy(FebsUtil.getCurrentUser().getUsername());
        this.save(subject);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSubject(Subject subject) {
        subject.setModifyTime(new Date());
        subject.setModifyBy(FebsUtil.getCurrentUser().getUsername());
        this.saveOrUpdate(subject);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSubject(String subjectId) {
        List<String> list = Arrays.asList(subjectId.split(Strings.COMMA));
        list.forEach(e -> {
            Subject subject = new Subject();
            subject.setId(Long.parseLong(e));
            subject.setIsDeleted(1);
            subject.setModifyBy(FebsUtil.getCurrentUser().getUsername());
            subject.setModifyTime(new Date());
            this.baseMapper.updateById(subject);
        });
	}
}
